import { Dialog } from 'vant'
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    meta: {
      keepAlive: false
    },
    component: () => import('../views/main/login/login.vue')
  },
  {
    path: '/register',
    name: 'register',
    meta: {
      keepAlive: false
    },
    component: () => import('../views/main/register/register.vue')
  },
  {
    path: '/search',
    name: 'search',
    meta: {
      keepAlive: true
    },
    component: () => import('../views/main/search/search.vue')
  },
  {
    path: '/product/:id',
    name: 'product',
    meta: {
      keepAlive: false
    },
    component: () => import('../views/main/product/product.vue')
  },
  {
    path: '/publish/:id',
    name: 'publish',
    meta: {
      keepAlive: true
    },
    component: () => import('../views/main/publish/publish.vue')
  },
  {
    path: '/profile',
    name: 'profile',
    meta: {
      keepAlive: false,
      requireAuth: true
    },
    component: () => import('../views/main/profile/profile.vue')
  },
  {
    path: '/about',
    name: 'about',
    meta: {
      keepAlive: false
    },
    component: () => import('../views/main/about/about.vue')
  },
  {
    path: '/add-product',
    name: 'add-product',
    meta: {
      keepAlive: false
    },
    component: () => import('../views/main/add-product/add-product.vue')
  },
  {
    path: '/add-article',
    name: 'add-article',
    meta: {
      keepAlive: false
    },
    component: () => import('../views/main/add-article/add-article.vue')
  },
  {
    path: '/article/:id',
    name: 'article',
    meta: {
      keepAlive: false
    },
    component: () => import('../views/main/article/article.vue')
  },
  {
    path: '/',
    name: 'layout',
    redirect: '/home',
    component: () => import('../views/layout/layout.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          keepAlive: true
        },
        component: () => import('../views/main/home/home.vue')
      },
      {
        path: '/found',
        name: 'found',
        meta: {
          keepAlive: true
        },
        component: () => import('../views/main/found/found.vue')
      },
      {
        path: '/collect',
        name: 'collect',
        meta: {
          keepAlive: true,
          requireAuth: true
        },
        component: () => import('../views/main/collect/collect.vue')
      },
      {
        path: '/me',
        name: 'me',
        meta: {
          keepAlive: true
        },
        component: () => import('../views/main/me/me.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.name === 'login' || !to.meta.requireAuth) {
    return next()
  }
  if (token) {
    return next()
  }
  Dialog.confirm({
    message: '该功能需要登录,确认登录吗?',
    confirmButtonColor: '#228efc'
  })
    .then(() => {
      return next('/login')
    })
    .catch(() => {
      return false
    })
})

export default router
